<?php
namespace app\common\service;

use app\common\logic\CartLogic;
use app\common\logic\GoodsLogic;
use app\common\logic\UsersLogic;
use think\Page;
use app\common\logic\wechat\WechatUtil;
use think\facade\Db;

class Address extends Base {
    //static $AUTH_CHECK = false; //默认必须登录

    public function lists(){

        $address_lists = get_user_address_list($this->user_id);
        if($address_lists){
            $area_arr = Db::name('region')->column('name','id');
            foreach($address_lists as $k=>$v){
                $v['province_name'] = $area_arr[$v['province']]??'';
                $v['city_name'] = $area_arr[$v['city']]??'';
                $v['district_name'] = $area_arr[$v['district']]??'';
                $v['town_name'] = $area_arr[$v['town']]??'';
                $v['area'] = $v['province_name'].','.$v['city_name'].','.$v['district_name'];
                $address_lists[$k] = $v;
            }
        }


        $result = [
            'status' => true,
            'msg' => '',
            'data' => $address_lists
        ];
        return $result;
    }
    public function info(){
        $id = input('id');
        $address = Db::name('user_address')->where(array('address_id' => $id, 'user_id' => $this->user_id))->find();
        $area_arr = Db::name('region')->column('name','id');
        if($address){
            $address['province_name'] = $area_arr[$address['province']]??'';
            $address['city_name'] = $area_arr[$address['city']]??'';
            $address['district_name'] = $area_arr[$address['district']]??'';
            $address['town_name'] = $area_arr[$address['town']]??'';
        }else{
            $address['province_name'] = '';
            $address['city_name'] = '';
            $address['district_name'] = '';
            $address['town_name'] = '';
        }

        $result = [
            'status' => true,
            'msg' => '',
            'data' => $address
        ];
        return $result;
    }
    public function add(){
        $post_data = input();
        $area = $post_data['area'];
        $area_arr = explode(',',$area);
        $area_data = [];
        $area_data['province'] = $area_arr[0];
        $area_data['city'] = $area_arr[1];
        $area_data['district'] = $area_arr[2];
        $area_data['consignee'] = $post_data['consignee']??'';
        $area_data['mobile'] = $post_data['mobile']??'';
        $area_data['address'] = $post_data['address']??'';
        $logic = new UsersLogic();
        $data = $logic->add_address($this->user_id, input('addressid'), $area_data);
        if($data['status']!=1){
            $result = [
                'status' => false,
                'msg' => $data['msg'],
                'data' => false
            ];
            return $result;
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'id' => $data['result']
            ]
        ];
        return $result;
    }

    public function edit(){
       $id = input('id');
        $post_data = input('');
        $logic = new UsersLogic();
        $data = $logic->add_address($this->user_id, $id, $post_data);
        if($data['status']!=1){
            $result = [
                'status' => false,
                'msg' => $data['msg'],
                'data' => false
            ];
            return $result;
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => true
        ];
        return $result;
    }

    public function del_address()
    {
        $id = input('addressid');

        $address = Db::name('user_address')->where("address_id", $id)->find();
        $row = Db::name('user_address')->where(array('user_id' => $this->user_id, 'address_id' => $id))->delete();
        // 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
        if ($address['is_default'] == 1) {
            $address2 = Db::name('user_address')->where("user_id", $this->user_id)->find();
            $address2 && Db::name('user_address')->where("address_id", $address2['address_id'])->update(array('is_default' => 1));
        }
        if (!$row){
            $result = [
                'status' => false,
                'msg' => '删除失败',
                'data' => []
            ];
            return $result;
        }else{
            $result = [
                'status' => true,
                'msg' => '删除成功',
                'data' => []
            ];
            return $result;
        }
    }

    public function getregion(){
        //$lists = Db::name('region')->where('1=1')->field('id as value,name')->select();
        //$lists = file_get_contents(ROOT_PATH.'/static/area.json');
        $lists = file_get_contents(ROOT_PATH."public/static/locationJson.js");
        $result = [
            'status' => true,
            'msg' => '',
            'data' => json_decode($lists,true)
        ];
        return $result;
    }

    public function setdefault(){
        $id = input('addressid');
        $is_default = Db::name('user_address')->where('address_id',$id)->value('is_default');
        $new_is_default = $is_default?0:1;
        Db::name('user_address')->where('address_id',$id)->update(['is_default'=>$new_is_default]);
        if($new_is_default){
            Db::name('user_address')->where(['address_id'=>['neq',$id],'user_id'=>$this->user_id])->update(['is_default'=>0]);
        }
        return ['status'=>1,'msg'=>''];
    }
}
